<?php

namespace p5\objects\ext;

use p5\objects\ArrayUtils;
use p5\objects\HtmlUtils;
use p5\objects\SessionUtils;

/**
 * Created by PhpStorm.
 * User: simon
 * Date: 2016/3/10
 * Time: 14:03
 */
class AdminDesignHtmlUtils extends HtmlUtils
{
    public static function make_boolean_radio($name, $defaultValue = 0, $attributes = array())
    {
        $matrix = array(
            "1" => "是"
        , "0" => "否"
        );
        return self::make_radio($name, $matrix, $defaultValue, $attributes, 6);
    }

    public static function make_radio($name, $matrix, $defaultValue = "", $attributes = array(), $cols = 6)
    {
        if (count($matrix) == 0) {
            return "";
        }
        $string = "";
        $string .= '<div class="option-group field">';
        $string .= '    <div class="row col-sm-12">';

        $attributes["name"] = $name;
        $attributes["id"] = $name;
        $attributes["type"] = "radio";

        $i = 0;
        foreach ($matrix as $key => $value) {
            $attributes["value"] = $key;
            $attributes["id"] = $name . "_" . $key;
            if ("a" . $key == "a" . $defaultValue || "a" . $value == "a" . $defaultValue) {
                $attributes["checked"] = true;
            } else {
                $attributes["checked"] = false;
            }
            $col_css = "col-sm-" . $cols;
            $string .= "<div class='h26 $col_css'>";
            $string .= '<label class="option option-primary">';
            $string .= tag("input", $attributes);
            $string .= '<span class="radio"></span>';
            $string .= "<span class='radio-text'>$value</span>";
            $string .= '</label>';
            $string .= '</div>';
            $i++;
        }

        $string .= '    </div>';
        $string .= '</div>';
        return $string;
    }

    public static function make_checkbox($name, $matrix, $defaultValues = [], $attributes = array(), $cols = 3)
    {
        if (count($matrix) == 0) {
            return "";
        }
        $string = "";
        $string .= '<div class="option-group field">';
        $string .= '    <div class="row col-sm-12">';
        $simple = str_replace("[]", "", $name);
        $attributes["name"] = $name;
        $attributes["id"] = $name;
        $attributes["type"] = "checkbox";

        $i = 0;
        foreach ($matrix as $key => $value) {
            $attributes["value"] = $key;
            $attributes["id"] = $simple . "_" . $key;
            if (in_array($key, $defaultValues)) {
                $attributes["checked"] = true;
            } else {
                $attributes["checked"] = false;
            }
            $col_css = "col-sm-" . $cols;
            $string .= "<div class='h26 $col_css'>";
            $string .= '<label class="option option-primary">';
            $string .= tag("input", $attributes);
            $string .= '<span class="checkbox"></span>';
            $string .= "<span class='radio-text'>$value</span>";
            $string .= '</label>';
            $string .= '</div>';
            $i++;
        }

        $string .= '    </div>';
        $string .= '</div>';
        return $string;
    }

    public static function make_select($name, $matrix, $defaultValues = "", $nullable = 1, $emptyLabel = "", $attributes = [])
    {
        if (!isset($attributes["class"])) {
            $attributes["class"] = "form-control";
        }
        return HtmlUtils::make_select($name, $matrix, $defaultValues, $nullable, $emptyLabel, $attributes);
    }

    static public function make_date_input($name, $attributes = [])
    {
        ArrayUtils::fill_blank($attributes, "dateFormat", "yy-mm-dd");
        ArrayUtils::fill_blank($attributes, "icon", "fa fa-calendar-o");
        ArrayUtils::fill_blank($attributes, "placeholder", "点击这里选择日期");
        return self::make_input($name, $attributes);
    }

    static public function make_datetime_input($name, $attributes = [])
    {
        ArrayUtils::fill_blank($attributes, "timeFormat", "yy-mm-dd");
        ArrayUtils::fill_blank($attributes, "icon", "fa fa-calendar-o");
        ArrayUtils::fill_blank($attributes, "placeholder", "点击这里选择时间");
        return self::make_input($name, $attributes);
    }

    public static function make_input($name, $attributes = [])
    {
        $properties = ["class" => "form-control"];
        $properties["id"] = $name;
        $properties["name"] = $name;
        $properties["type"] = "text";
        $properties["value"] = SessionUtils::deep_get($name, true, "");
        $properties = array_merge($properties, $attributes);
        $string = self::tag("input", $properties);

        if (array_key_exists("icon", $attributes)) {
            $icon = $attributes["icon"];
            if (!empty($icon)) {
                $string = "
                    <label class='field prepend-icon'>
                        $string
                        <label class='field-icon'>
                            <i class='$icon'></i>
                        </label>
                    </label>
                ";
            }
        }
        return $string;
    }
}